/*******************************************************************************
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Jean Neveux - initial API and implementation
 *******************************************************************************/
package org.dynamo.database.ui.wizzard.launcher;

import org.dynamo.database.ui.wizzard.DTUImage;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
import org.eclipse.debug.ui.AbstractLaunchConfigurationTab;
import org.eclipse.debug.ui.ILaunchConfigurationTab;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.BusyIndicator;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.PlatformUI;

/**
 * Database Reverse Launcher tab. 
 * Mainly used to select a configuration file and launch a reverse.
 * @author J.Neveux
 *
 */
public class MainTab extends AbstractLaunchConfigurationTab {

	private Text configPath;
	private Image tabImage;
	@Override
	public void createControl(final Composite parent) {
		Display.getDefault().syncExec(new Runnable() {
			public void run() {
				tabImage = DTUImage.IMG_DTU.createImage();
				Composite mainComposite = new Composite(parent, SWT.NONE);
				setControl(mainComposite);
				PlatformUI
						.getWorkbench()
						.getHelpSystem()
						.setHelp(
								getControl(),
								"org.eclipse.debug.ui.launch_configuration_dialog_environment_tab");
				GridLayout gd = new GridLayout(3,false);
				GridData gridData = new GridData(GridData.GRAB_HORIZONTAL);
				mainComposite.setLayout(gd);
				mainComposite.setLayoutData(gridData);
				Label test = new Label(mainComposite, SWT.NONE);
				test.setText("Configuration file: ");
				configPath = new Text(mainComposite, SWT.BORDER);
				GridData configData = new GridData(GridData.FILL_HORIZONTAL);
				configData.minimumWidth = 150;
				configPath.setLayoutData(configData);
				
				configPath.addModifyListener(new ModifyListener() {
					@Override
					public void modifyText(ModifyEvent e) {
						setDirty(true);
						updateLaunchConfigurationDialog();		
					}
				});
				
				Button selectConf = new Button(mainComposite, SWT.NONE);
				selectConf.setText("&Browse");
				selectConf.addSelectionListener(new SelectionAdapter() {
					public void widgetSelected(SelectionEvent e) {
						BusyIndicator.showWhile(parent.getDisplay(), new Runnable() {
							public void run() {
								handleOpenFile(parent);
							}
						});
					}
				});
				
			}
		});
	}

	@Override
	public String getName() {
		return "Database Reverse";
	}

	@Override
	public void initializeFrom(ILaunchConfiguration configuration) {
		try {
			configPath.setText(configuration.getAttribute("configPath", ""));
		} catch (CoreException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public void performApply(ILaunchConfigurationWorkingCopy configuration) {
		configuration.setAttribute("configPath", configPath.getText());
	}

	@Override
	public void setDefaults(ILaunchConfigurationWorkingCopy configuration) {
		configPath.setText("");
	}
	
	public boolean isValid()
	{
		if(configPath.getText().endsWith(".dtu"))
		{
			return true;
		}
		return false;
	}

	public void setConfigPath(Text configPath) {
		this.configPath = configPath;
	}

	public Text getConfigPath() {
		return configPath;
	}
	
	private void handleOpenFile(Composite parent) {
		SelectFileDiag dialog = new SelectFileDiag(parent.getShell(), getConfigPath().getText());
		dialog.create();
		dialog.getShell().setText("Select configuration file.");
		
		if(dialog.open() == Window.OK)
		{
			configPath.setText(dialog.getPlatformFile());
			setDirty(true);
			updateLaunchConfigurationDialog();
		}
	}
	
	/**
	 * @see ILaunchConfigurationTab#getImage()
	 */
	public Image getImage() {
		return tabImage;
	}
	
	public void dispose()
	{
		tabImage.dispose();
	}

}
